package com.zzy.forum.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.forum.mapper.BlogsMapper;
import com.zzy.forum.mapper.UserMapper;
import com.zzy.forum.model.Blogs;
import com.zzy.forum.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BlogsMapper blogsMapper;
    @GetMapping("/")
    public String index(Model model){
        QueryWrapper<Blogs> blogsQueryWrapper = new QueryWrapper<>();
        blogsQueryWrapper.orderByDesc("id");
        List<Blogs> blogs = blogsMapper.selectList(blogsQueryWrapper);
        model.addAttribute("blogs",blogs);
        return "index";
    }
    //注册
    @GetMapping("/getregister")
    public String getRegister(){
        return "user/register";
    }

    @PostMapping("/user/register")
    public String register(Model model,User user,String password_again ){
        if (!user.getPassword() .equals(password_again)){
            model.addAttribute("register","密码不一致");
            return "user/register";
        }
        user.setCount(0);
        user.setImgUrl("");
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("username",user.getUsername());
        User user1 = userMapper.selectOne(wrapper);
        if (user1 == null){
            userMapper.insert(user);
            model.addAttribute("register","注册成功请登录");
            return "user/login";
        }else {
            model.addAttribute("register","账号已存在");
            return "user/register";
        }
    }

    //登录
    @GetMapping("/getlogin")
    public String getLogin(){
        return "user/login";
    }
    @PostMapping("/user/login")
    public String login(Model model, String username, String password, HttpServletResponse response){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("password",password);
        User user = userMapper.selectOne(wrapper);
        if (user != null){
            Cookie cookie = new Cookie("user_id",String.valueOf(user.getId()));
            cookie.setMaxAge(60*60*24);
            cookie.setPath("/");
            response.addCookie(cookie);

            return "redirect:/";
        }else {
            model.addAttribute("login","账号不存在或密码错误");
            return "user/login";
        }
    }

    //退出
    @GetMapping("/exit")
    public String exit(Model model ,HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                cookie.setMaxAge(0);
                break;
            }
        }
        return "user/login";
    }

    //修改个人信息
    @GetMapping("/getmyselfinfo")
    public String getSelfInfo(Model model,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        User user1 = userMapper.selectById(userId);
        model.addAttribute("mes","修改成功");
        model.addAttribute("username",user1.getUsername());
        return "user/myself_info";
    }
    @PostMapping("/user/myselfinfo")
    public String myselfinfo(Model model, User user,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id",userId);
        userMapper.update(user,userQueryWrapper);
        model.addAttribute("mes","修改成功");
        return "user/myself_info";
    }
}
